package com.example.testgit.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.testgit.VO.PageResult;
import com.example.testgit.VO.ParentVO;
import com.example.testgit.VO.StudentBasicVO;
import com.example.testgit.VO.StudentVO;
import com.example.testgit.entity.BedDO;
import com.example.testgit.entity.CourseDO;
import com.example.testgit.entity.ParentDO;
import com.example.testgit.entity.StudentDO;
import com.example.testgit.listener.StudentDOListener;
import com.example.testgit.mapper.BedMapper;
import com.example.testgit.mapper.CourseMapper;
import com.example.testgit.mapper.ParentMapper;
import com.example.testgit.mapper.StudentMapper;
import com.example.testgit.mapstruct.ParentConverter;
import com.example.testgit.mapstruct.StudentBasicConverter;
import com.example.testgit.mapstruct.StudentConverter;
import com.example.testgit.service.StudentService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: lyh
 * @create: 2023/3/15 11:11
 * Description：
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentMapper studentMapper;

    @Resource
    private ParentMapper parentMapper;

    @Resource
    private BedMapper bedMapper;

    @Resource
    private CourseMapper courseMapper;

    @Override
    public PageResult<StudentVO> search(String stuName, Integer stuId, String major, String classNum,
                                        Integer pageNum, Integer pageSize) {
        // 查询条件
        QueryWrapper<StudentDO> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(stuName))
            wrapper.like("stu_name", stuName);
        if (StringUtils.isNotBlank(major))
            wrapper.eq("major", major);
        if (StringUtils.isNotBlank(classNum))
            wrapper.eq("class_num", classNum);
        if (stuId != null)
            wrapper.eq("stu_id", stuId);

        // 分页查询
        Page<StudentDO> page = new Page<>(pageNum, pageSize);
        Page<StudentDO> resDo = studentMapper.selectPage(page, wrapper);
        PageResult pageResult = new PageResult(resDo);
        pageResult.setData(StudentConverter.INSTANCE.toVOList(resDo.getRecords()));

        return pageResult;
    }

    @Override
    public StudentBasicVO basicInfor(Integer stuId) {

        QueryWrapper<StudentDO> wrapper = new QueryWrapper<>();
        wrapper.eq("stu_id", stuId);

        StudentDO studentDo = studentMapper.selectOne(wrapper);
        // do -> vo
        StudentBasicVO studentBasicVO = StudentBasicConverter.INSTANCE.toVO(studentDo);

        // 学期晚归和未归次数
        QueryWrapper<BedDO> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("stu_id",stuId);
        wrapper2.gt("bed_type",0);
        List<BedDO> bedDOListedDO = bedMapper.selectList(wrapper2);
        int lateCount = 0;
        int notCount = 0;
        for (int i = 0; i < bedDOListedDO.size(); i++){
            if(bedDOListedDO.get(i).getBedType() == 1){
                lateCount++;
            }else if(bedDOListedDO.get(i).getBedType() == 2){
                notCount++;
            }
        }
        if(lateCount >= 5 || notCount >= 2){
            studentBasicVO.setBedWarn("归寝预警");
        }else {
            studentBasicVO.setBedWarn("");
        }

        // 学业预警计算
        QueryWrapper<CourseDO> courseWrapper = new QueryWrapper<>();
        courseWrapper.eq("stu_id", stuId);
        List<CourseDO> compute = courseMapper.selectList(courseWrapper).stream().filter(obj -> obj.getIsCount() == 1).collect(Collectors.toList());
        int totalClass = compute.size();
        int totalScore = 0;
        for (CourseDO course : compute) totalScore += course.getCourseScore();
        double gpa = totalScore * 1.0 / totalClass / 100 * 4;
        if (gpa < 1.0) studentBasicVO.setStudyWarn("学业预警");
        else studentBasicVO.setStudyWarn(null);

        return studentBasicVO;
    }



    @Override
    public ParentVO parentInfor(Integer stuId){

        QueryWrapper<ParentDO> wrapper = new QueryWrapper<>();
        wrapper.eq("stu_id",stuId);

        ParentDO parentDO = parentMapper.selectOne(wrapper);

        ParentVO parentVO = ParentConverter.INSTANCE.toVO(parentDO);

        return parentVO;
    }

    @Override
    public boolean insert(StudentDO stu) {
        int insert = studentMapper.insert(stu);
        if (insert > 0) return true;
        else return false;
    }

    @Override
    public boolean update(StudentDO stu) {
        UpdateWrapper<StudentDO> wrapper = new UpdateWrapper<>();
        wrapper.eq("stu_id", stu.getStuId());
        int res = studentMapper.update(stu, wrapper);
        if (res > 0) return true;
        else return false;
    }

    @Override
    public boolean delete(Integer stuId) {
        QueryWrapper<StudentDO> wrapper = new QueryWrapper<>();
        wrapper.eq("stu_id", stuId);
        int res = studentMapper.delete(wrapper);
        if (res > 0) return true;
        else return false;
    }

    @Override
    public void excleIn(MultipartFile file){
        String fileName = "D:\\desktop\\" + file.getOriginalFilename();
        System.out.println(fileName);
        EasyExcel.read(fileName, StudentDO.class, new StudentDOListener(studentMapper)).sheet().doRead();
    }


}
